package com.zzf.eduservice.controller;


import com.alibaba.excel.util.StringUtils;
import com.zzf.commonutils.R;
import com.zzf.eduservice.client.VodClient;
import com.zzf.eduservice.entity.EduVideo;
import com.zzf.eduservice.entity.chapter.VideoVo;
import com.zzf.eduservice.service.EduVideoService;
import com.zzf.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-05
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    public R addvideo(@RequestBody EduVideo eduVideo) {
        videoService.save(eduVideo);
        return R.ok();
    }

    //删除小节
    //TODO后期需要完善,删除小节时,同时把里边的视频删掉
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id) {

        //根据小节id获取视频id,然后调用删除视频的方法
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            R result = vodClient.removeAliVideo(videoSourceId);
            if (result.getCode() == 20001) {
                throw new GuliException(20001, "删除视频失败,熔断器");
            }
        }
        //删除小节
        boolean b = videoService.removeById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error().message("删除失败");
        }
    }

    //修改小节
    @PostMapping("updateVideo")
    public R updateVideo(EduVideo video){
        boolean update = videoService.updateById(video);
        if (update){
            return R.ok();
        }else {
            return R.error().message("修改失败");
        }
    }
}

